package com.rockville.ufone.rssdata;

import java.net.URL;
import java.util.Vector;

import org.apache.log4j.Logger;
import org.apache.log4j.helpers.Loader;
import org.apache.log4j.xml.DOMConfigurator;

import com.rockville.httpRequest.HttpRequestUrl;
import com.rockville.ufone.DBProc;

public class StockMarketParser implements Runnable {
	
	
	private static final Logger log = Logger.getLogger(StockMarketParser.class);
	private static final String mktFetchUrl = "http://www.weonline.biz/mkt/mktwfetch10.php";
	private static String mktCheckUrl = "http://www.weonline.biz/mkt/check.php?symbol=";	
		
    
    public StockMarketParser() {
    }

	@Override
	public void run() {		
		try {			
			/*
			String stockUpdatesData = "NPL;REG;OPN; ;10,000;13.23;13.29;30,000;13.22;1,000;15:29:01;12.84;+;13.32;13.73;12.80;0.38;6,738,874;1,056;*" +
"TELE;REG;OPN; ;4,999;2.43;2.46;10,000;2.45;10,000;15:29:21;2.20;-;2.46;2.55;2.20;0.25;3,506,575;852;*" +
"AKBL;REG;OPN; ;7,017;15.16;15.23;10,000;15.24;750;15:29:11;14.80;+;15.04;15.35;14.67;0.44;3,681,488;541;*" +
"AHSL;REG;OPN; ;6,107;22.76;22.78;1,590;22.76;200;15:29:33;23.16;-;23.18;23.49;22.75;-0.40;3,416,804;1,976;*" +
"WTL;REG;OPN; ;45,000;2.82;2.84;1,950;2.82;13,386;15:29:00;2.83;-;2.88;2.94;2.75;-0.01;3,920,867;685;*" +
"JSCL;REG;OPN; ;1,080;9.54;9.55;23,814;9.54;5,000;15:29:30;9.57;+;9.68;9.81;9.51;-0.03;6,168,341;2,750;*" +
"NML;REG;OPN; ;500;50.16;50.19;1,300;50.19;200;15:29:32;49.58;-;50.06;50.40;49.65;0.61;4,436,145;2,770;*" +
"ATRL;REG;OPN; ;2,900;91.26;91.40;2,800;91.40;300;15:29:31;87.96;+;90.52;91.94;88.32;3.44;3,337,320;4,716;*" +
"LOTPTA;REG;OPN; ;22,500;9.32;9.33;1,500;9.33;5,000;15:29:15;8.88;+;9.21;9.39;8.90;0.45;19,280,876;3,417;*" +
"DGKC;REG;OPN; ;22,500;25.76;25.80;500;25.76;1,000;15:29:28;25.72;-;25.90;26.00;25.70;0.04;3,752,019;1,411;*" +
"|" +
"KSE30;10,068.30;64,050,373;3,299,231,297.06;10,166.10;10,066.50;-1.17;*" +
"KSE100;10,401.30;90,426,903;3,647,471,568.98;10,476.30;10,388.80;11.36;*" +
"ALLSHR;7,247.61;115,332,501;3,890,071,468.69;7,295.94;7,237.62;9.99;*";
*/
			
			String stockUpdatesData = "";
			String mktShares = HttpRequestUrl.getURLResponse(mktFetchUrl);
			//PSAF,AMTEX,JOVC,KESC,SILK,TELE,PASL,NPL,ANL,NBP
			if(!"".equals(mktShares)) {
				log.debug("Stock market shares found: "+mktShares);
				stockUpdatesData = HttpRequestUrl.getURLResponse(mktCheckUrl+mktShares);
				log.debug("Stock market data found: "+stockUpdatesData);
			}
			if(!"".equals(stockUpdatesData)) {
				DBProc db = DBProc.getInstance();
				String [] data = stockUpdatesData.split("\\|");
				String stockUpdatesRaw = data[0];
				String indexRatingRaw = data[1];
				
				String [] stockUpdates = stockUpdatesRaw.split("\\*");
				String [] indexRating = indexRatingRaw.split("\\*");
				Vector<String []> vec = new Vector<String []>();
				for(int index = 0; index<stockUpdates.length; index++) {
					String [] sharesData = stockUpdates[index].split(";");
//					log.debug("sharesData size: "+sharesData.length);
					if(sharesData.length >= 18) {
						String [] dbData = new String[]{
								sharesData[0], //symbol 
								sharesData[3], //flag
								sharesData[4], //bid vol								
								sharesData[5], //bid price 
								sharesData[6], //offer price
								sharesData[7], //offer vol 
								sharesData[8], //last rate 
								sharesData[16],//change 
								sharesData[17] //total vol
								           };
						vec.add(dbData);
						log.debug("new stock updates data {"+dbData[0]+","+dbData[1]+","+dbData[2]+","+dbData[3]+"," +
								""+dbData[4]+","+dbData[5]+","+dbData[6]+","+dbData[7]+","+dbData[8]+"}");
					}
				}
				db.updateStockMarketRates(vec);
				
				vec = new Vector<String []>();
				for(int index = 0; index<indexRating.length; index++) {
					String [] indexData = indexRating[index].split(";");															
					if(indexData.length >= 7) {						
						vec.add(indexData);
						log.debug("new index data {"+indexData[0]+","+indexData[1]+","+indexData[2]+","+indexData[3]+"," +
								""+indexData[4]+","+indexData[5]+","+indexData[6]+"}");
						/*
						String [] dbData = new String[]{indexData[0], indexData[1], indexData[2], indexData[3], 
								indexData[4], indexData[5], indexData[6]};
						vec.add(dbData);
						*/
					}					
				}
				db.updateStockIndexes(vec);				
			}
			
		} catch (Exception ex) {
			log.error("", ex);
		}		
	}

	public static void main(String[] args) {
		try {
			URL url = Loader.getResource("log4j-config.xml");
			DOMConfigurator.configure(url);
			StockMarketParser parser = new StockMarketParser();
			parser.run();
//			ExecutorService executor = Executors.newCachedThreadPool();
//			executor.execute(parser);
		} catch (Exception e) {
			log.error("", e);
		}
	}
	
}
